package com.tony.mindhr.controller.emp;

import com.tony.mindhr.model.*;
import com.tony.mindhr.service.*;
import com.tony.mindhr.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: EmpBasicController
 * @Auther: 皮蛋布丁
 * @Date: 2021/06/13/20:24
 * @Description:
 */
@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;

    /**
    * @Description: getEmployeeByPage 获取员工列表信息(分页)
    * @Param: [page, size]
    * @return: com.tony.mindhr.model.RespPageBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/13 20:33
    */
    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue =
            "10") Integer size, Employee employee, Date[] beginDateScope) {
        return employeeService.getEmployeeByPage(page,size,employee,beginDateScope);
    }

    /**
    * @Description: addEmp 添加员工
    * @Param: [employee]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 16:10
    */
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee) {
        if (employeeService.addEmp(employee) == 1) {
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    /**
    * @Description: getAllNations 获取民族下拉列表
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Nation>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 20:21
    */
    @GetMapping("/nations")
    public List<Nation> getAllNations() {
        return nationService.getAllNations();
    }

    /**
    * @Description: getAllPoliticsstatus 获取政治面貌下拉列表
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Politicsstatus>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 20:26
    */
    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsstatus() {
        return politicsstatusService.getAllPoliticsstatus();
    }

    /**
    * @Description: getAllJobLevels 获取职称下拉列表
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.JobLevel>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 20:27
    */
    @GetMapping("/joblevels")
    public List<JobLevel> getAllJobLevels() {
        return jobLevelService.getAllJobLevels();
    }

    /**
    * @Description: getAllPositions 获取职位下拉列表
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Position>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 20:47
    */
    @GetMapping("/positions")
    public List<Position> getAllPositions() {
        return positionService.getAllPositions();
    }

    /**
    * @Description: maxworkID 获取最大工号
    * @Param: []
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 21:08
    */
    @GetMapping("/maxworkid")
    public RespBean maxworkID() {
//        return RespBean.ok("",String.format("%08d",employeeService.maxworkID() + 1));
        RespBean respBean = RespBean.build().setStatus(200)
                .setObj(String.format("%08d", employeeService.maxworkID() + 1));
        return respBean;
    }

    /**
    * @Description: getAllDepartments 获取部门树
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Department>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 21:54
    */
    @GetMapping("/deps")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }

    /**
    * @Description: deleteEmpById 根据id删除员工
    * @Param: [id]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/15 21:56
    */
    @DeleteMapping("/{id}")
    public RespBean deleteEmpById(@PathVariable Integer id) {
        if (employeeService.deleteEmpById(id) == 1) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    /**
    * @Description: updateEmp 更新员工
    * @Param: [employee]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/16 20:41
    */
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee) {
        if (employeeService.updateEmp(employee) == 1) {
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    /**
    * @Description: exportData 导出员工数据excel
    * @Param: []
    * @return: org.springframework.http.ResponseEntity<byte[]>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/17 22:33
    */
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        List<Employee> list = (List<Employee>) employeeService.getEmployeeByPage(null,null,null,null).getData();
        return POIUtils.employee2Excel(list);

    }

    /**
    * @Description: importData 文件上传
    * @Param: [file]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/18 23:01
    */
    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        List<Employee> list = POIUtils.employee2Employ(file,nationService.getAllNations(),
                politicsstatusService.getAllPoliticsstatus(),departmentService.getAllDepartmentsWithOutChildren(),
                positionService.getAllPositions(),
                jobLevelService.getAllJobLevels());
        if (employeeService.addEmps(list) == list.size()) {
            return RespBean.ok("上传成功！");
        }
        return RespBean.error("上传失败！");
    }




}
